Left Shift(<<)

It is a binary operator that takes two numbers, left shifts the bits of the first operand, and the second operand decides the number of places to shift. In other words, left-shifting an integer “a” with an integer “b” denoted as ‘(a<<b)’ is equivalent to multiplying a with 2^b (2 raised to power b). 

Syntax:

a << b;
  • a: First Operand
  • b: Second Operand

Example: Let’s take a=5; which is 101 in Binary Form. Now, if “a is left-shifted by 2” i.e a=a<<2 then a will become a=a*(2^2). Thus, a=5*(2^2)=20 which can be written as 10100.

 


C




// C Program to demonstrate use
// of left shift  operator
#include <stdio.h>
 
// Driver code
int main()
{
    // a = 5(00000101), b = 9(00001001)
    unsigned char a = 5, b = 9;
 
    // The result is 00001010
    printf("a<<1 = %d\n", (a << 1));
 
    // The result is 00010010
    printf("b<<1 = %d", (b << 1));
    return 0;
}


C++




// C++ Program to demonstrate use
// of left shift  operator
#include <iostream>
using namespace std;
 
// Driver code
int main()
{
    // a = 5(00000101), b = 9(00001001)
    unsigned char a = 5, b = 9;
 
    // The result is 00001010
    cout << "a<<1 = " << (a << 1) << endl;
 
    // The result is 00010010
    cout << "b<<1 = " << (b << 1) << endl;
    return 0;
}


Output

a<<1 = 10
b<<1 = 18

Left Shift and Right Shift Operators in C/C++

Similar Reads

Left Shift(<<)

It is a binary operator that takes two numbers, left shifts the bits of the first operand, and the second operand decides the number of places to shift. In other words, left-shifting an integer “a” with an integer “b” denoted as ‘(a<

Right Shift(>>)

...

Important Points

...

Contact Us